package Order.Entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@NamedQueries({
    @NamedQuery(name = "Customer.findAll", query = "select o from Customer o"),
    @NamedQuery(name = "Customer.findCustomerById", query = "select o from Customer o where o.id = :id")
})
@Table(name = "customers")
public class Customer implements Serializable {

    @Id
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "address")
    private String address;
    @Column(name = "contact")
    private Integer contact;
    @OneToMany(mappedBy = "customer")
    private List<Order> orders;

    public Integer getId() {
        return id;
    }

    public void setId(Integer cus_id) {
        this.id = cus_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String cus_name) {
        this.name = cus_name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String cus_address) {
        this.address = cus_address;
    }

    public Integer getContact() {
        return contact;
    }

    public void setContact(Integer cus_contact) {
        this.contact = cus_contact;
    }

    @XmlTransient
    public List<Order> getOrders() {
        if(orders == null)
        {
            orders = new ArrayList<Order>();
        }
        return orders;
    }

    public void setOrders(List<Order> ordersList) {
        this.orders = ordersList;
    }

    public Order addOrder(Order order) {
        getOrders().add(order);
        order.setCustomer(this);
        return order;
    }

    public Order removeOrder(Order order) {
        getOrders().remove(order);
        order.setCustomer(null);
        return order;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ((this.getId() == null && other.getId() != null)
                || (this.getId() != null && !this.getId().equals(other.getId()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Edu.Order.Customer[id=" + this.getId() + "]";
    }
}
